package com.kuangxiaoyang.foodapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.kuangxiaoyang.foodapp.pojo.Food;
import com.squareup.picasso.Picasso;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class FoodDetailActivity extends AppCompatActivity {

    private static final String TAG = "FoodDetailActivity";

    private TextView foodNameTv;
    private TextView foodPriceTv;
    private TextView foodDescTv;
    private ImageView foodImg;
    private Button showCommenBtn;
    private String foodName;
    private Food.DataBean data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_detail);
        initView();
        showFoodDetail();
        initEvent();
    }

    private void showFoodDetail() {
        Intent intent = getIntent();
        final String foodId = intent.getStringExtra("foodId");
        final int position = intent.getIntExtra("position", 0);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient().newBuilder()
                            .build();
                    Request request = new Request.Builder()
                            .url("http://192.168.137.1:8083/foodserver/food/findone/" + foodId)
                            .method("GET", null)
                            .build();

                    Response response = client.newCall(request).execute();
                    String result = response.body().string();
                    Log.e(TAG, "run: " + result);


                    Gson gson = new Gson();
                    data = gson.fromJson(result, Food.class).getData();

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            foodNameTv.setText(data.getName());
                            foodDescTv.setText(data.getDescription());
                            foodPriceTv.setText("价格：" + data.getPrice() + "元");
                            String iconName = data.getIcon();

                            Picasso.with(FoodDetailActivity.this).load("http://192.168.137.1:8083/img/" + iconName).into(foodImg);

                        }
                    });


                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void initEvent() {
        showCommenBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FoodDetailActivity.this, CommentsListActivity.class);
                intent.putExtra("foodName", foodName);
                intent.putExtra("foodId",String.valueOf(data.getId()));
                startActivity(intent);
            }
        });

    }


    private void initView() {

        foodImg = findViewById(R.id.de_food_img);
        foodNameTv = findViewById(R.id.de_food_name_tv);
        foodPriceTv = findViewById(R.id.de_food_price_tv);
        foodDescTv = findViewById(R.id.de_food_desc_tv);

        showCommenBtn = findViewById(R.id.show_comment_btn);

    }
}
